/**
 * @author   Ivan Andonov
 * @email:   ivan.andonov[at]gmail[dot]com
 *
 * @require  sjs 
 * @use  
 * @optional 
 **/

(function() {
	sjs.create('sjs.Xml', {
		
		// PUBLIC STATIC METHODS
		getRoot : function(_xml) {
			return this.getFirstChild(_xml);
		},
		
		getFirstChild : function(_node) {
			if (_node && _node.childNodes) {
				for (var i = 0; i < _node.childNodes.length; i++) {
					switch (_node.childNodes[i].nodeType) {
						case 1: // node
						case 4: // CDATA
							return _node.childNodes[i];
					}
				}
			}
			return null;
		},
		
		getNextSibling : function(_node) {
			while (_node.nextSibling != null) {
				_node = _node.nextSibling;
				if (_node.nodeType == 1) break;
			}
			return _node;
		},
		
		getPrevSibling : function(_node) {
			while (_node.previousSibling != null) {
				_node = _node.previousSibling;
				if (_node.nodeType == 1) break;
			}
			return _node;
		},
		
		removeVersion : function(str) {
			return str.substr(str.indexOf("<", str.indexOf("?>")));
		},
		
		nodeToString : function(_node) {
			var node = document.createElement("div");
			node.appendChild(_node.cloneNode(true));
			return node.innerHTML;
		},
		
		getNodeByNodeName : function(_node, nodeName) {
			for (_node = _node.firstChild; _node !== null; _node = _node.nextSibling) {
				if (_node.nodeName == nodeName) {
					return _node;
				}
			}
			return null;
		},
		
		getNodesByNodeName : function(_node, nodeName, start, limit) {
			var result = [];
			var count = 0;
			var len = _node.childNodes.length;
			start = Math.max(0, start || 0);
			limit = Math.max(0, limit || len);
			for (var i = 0; i < len; i++) {
				if (_node.childNodes[i].nodeName == nodeName) {
					count++;
					if (++count >= start && result.length < len) {
						result.push(_node.childNodes[i]);
						if (result.length >= len) {
							break;
						}
					}
				}
			}
			return result;
		},
		
		getNodeByAttribute : function(_node, attr) {
			for (_node = _node.firstChild; _node !== null; _node = _node.nextSibling) {
				if (this.getAttributeValue(_node, attr) != null) {
					return _node;
				}
			}
			return null;
		},
		
		getNodesByAttribute : function(_node, attr, start, limit) {
			var result = [];
			var count = 0;
			var len = _node.childNodes.length;
			start = Math.max(0, start || 0);
			limit = Math.max(0, limit || len);
			for (var i = 0; i < len; i++) {
				if (this.getAttributeValue(_node.childNodes[i], attr) !== null) {
					count++;
					if (++count >= start && result.length < len) {
						result.push(_node.childNodes[i]);
						if (result.length >= len) {
							break;
						}
					}
				}
			}
			return result;
		},
		
		getNodeByAttributeValue : function(_node, attr, value) {
			for (_node = _node.firstChild; _node !== null; _node = _node.nextSibling) {
				if (this.getAttributeValue(_node, attr) == value) {
					return _node;
				}
			}
			return null;
		},
		
		getNodesByAttributeValue : function(_node, attr, value, start, limit) {
			var result = [];
			var count = 0;
			var len = _node.childNodes.length;
			start = Math.max(0, start || 0);
			limit = Math.max(0, limit || len);
			for (var i = 0; i < len; i++) {
				if (this.getAttributeValue(_node.childNodes[i], attr) === value) {
					count++;
					if (++count >= start && result.length < len) {
						result.push(_node.childNodes[i]);
						if (result.length >= len) {
							break;
						}
					}
				}
			}
			return result;
		},
		
		getAttributeValue : function(_node, attr) {
			return _node.attributes[attr].value;
		}
		
	});
	
})();